package com.bao.auth.server.biz.service.auth.impl;

import com.bao.auth.entity.auth.Menu;
import com.bao.auth.server.biz.dao.auth.MenuMapper;
import com.bao.auth.server.biz.service.auth.MenuService;
import com.bao.auth.server.biz.service.auth.ResourceService;
import com.bao.core.utils.NumberHelper;
import static com.bao.core.utils.StrPool.DEF_PARENT_ID;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper,Menu> implements MenuService {

    @Autowired
    private ResourceService resourceService;

    @Override
    public boolean removeByIds(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)){
            return true;
        }
        boolean result = super.removeByIds(ids);
        if (result) {
            resourceService.removeByMenuId(ids);
        }
        return result;
    }

    @Override
    public List<Menu> findVisibleMenu(String group, Long userId) {
        List<Menu> visibleMenu = baseMapper.findVisibleMenu(group, userId);
        return visibleMenu;
    }

    @Override
    public boolean save(Menu menu) {
        menu.setIsEnable(NumberHelper.getOrDef(menu.getIsEnable(), true));
        menu.setIsPublic(NumberHelper.getOrDef(menu.getIsPublic(), false));
        //菜单父id为空就默认根节点
        menu.setParentId(NumberHelper.getOrDef(menu.getParentId(), DEF_PARENT_ID));
        return super.save(menu);
    }
}
